3 * Updater for link tracking tables after a page edit.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 use MediaWiki\MediaWikiServices
;
23 use Wikimedia\ScopedCallback
;
24 use Wikimedia\Rdbms\IDatabase
;
27 * Update object handling the cleanup of links tables after a page was deleted.
29 class LinksDeletionUpdate
extends DataUpdate
implements EnqueueableDataUpdate
{
41 * @param WikiPage $page Page we are updating
42 * @param int|null $pageId ID of the page we are updating [optional]
43 * @param string|null $timestamp TS_MW timestamp of deletion
46 function __construct( WikiPage
$page, $pageId = null, $timestamp = null ) {
47 parent
::__construct();
51 $this->pageId
= $pageId; // page ID at time of deletion
52 } elseif ( $page->exists() ) {
53 $this->pageId
= $page->getId();
55 throw new InvalidArgumentException( "Page ID not known. Page doesn't exist?" );
58 $this->timestamp
= $timestamp ?
: wfTimestampNow();
61 public function doUpdate() {
62 $services = MediaWikiServices
::getInstance();
63 $config = $services->getMainConfig();
64 $lbFactory = $services->getDBLoadBalancerFactory();
65 $batchSize = $config->get( 'UpdateRowsPerQuery' );
67 // Page may already be deleted, so don't just getId()
70 if ( $this->ticket
) {
71 // Make sure all links update threads see the changes of each other.
72 // This handles the case when updates have to batched into several COMMITs.
73 $scopedLock = LinksUpdate
::acquirePageLock( $this->getDB(), $id );
75 throw new RuntimeException( "Could not acquire lock for page ID '{$id}'." );
79 $title = $this->page
->getTitle();
80 $dbw = $this->getDB(); // convenience
82 // Delete restrictions for it
83 $dbw->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__
);
85 // Fix category table counts
86 $cats = $dbw->selectFieldValues(
92 $catBatches = array_chunk( $cats, $batchSize );
93 foreach ( $catBatches as $catBatch ) {
94 $this->page
->updateCategoryCounts( [], $catBatch, $id );
95 if ( count( $catBatches ) > 1 ) {
96 // Only sacrifice atomicity if necessary due to size
97 $lbFactory->commitAndWaitForReplication(
98 __METHOD__
, $this->ticket
, [ 'domain' => $dbw->getDomainID() ]
103 // Refresh counts on categories that should be empty now
104 if ( $title->getNamespace() === NS_CATEGORY
) {
105 // T166757: do the update after the main job DB commit
106 DeferredUpdates
::addCallableUpdate( function () use ( $title ) {
107 $cat = Category
::newFromName( $title->getDBkey() );
108 $cat->refreshCountsIfEmpty();
112 $this->batchDeleteByPK(
114 [ 'pl_from' => $id ],
115 [ 'pl_from', 'pl_namespace', 'pl_title' ],
118 $this->batchDeleteByPK(
120 [ 'il_from' => $id ],
121 [ 'il_from', 'il_to' ],
124 $this->batchDeleteByPK(
126 [ 'cl_from' => $id ],
127 [ 'cl_from', 'cl_to' ],
130 $this->batchDeleteByPK(
132 [ 'tl_from' => $id ],
133 [ 'tl_from', 'tl_namespace', 'tl_title' ],
136 $this->batchDeleteByPK(
138 [ 'el_from' => $id ],
142 $this->batchDeleteByPK(
144 [ 'll_from' => $id ],
145 [ 'll_from', 'll_lang' ],
148 $this->batchDeleteByPK(
150 [ 'iwl_from' => $id ],
151 [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
155 // Delete any redirect entry or page props entries
156 $dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__
);
157 $dbw->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__
);
159 // Find recentchanges entries to clean up...
160 $rcIdsForTitle = $dbw->selectFieldValues(
164 'rc_type != ' . RC_LOG
,
165 'rc_namespace' => $title->getNamespace(),
166 'rc_title' => $title->getDBkey(),
168 $dbw->addQuotes( $dbw->timestamp( $this->timestamp
) )
172 $rcIdsForPage = $dbw->selectFieldValues(
175 [ 'rc_type != ' . RC_LOG
, 'rc_cur_id' => $id ],
179 // T98706: delete by PK to avoid lock contention with RC delete log insertions
180 $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
181 foreach ( $rcIdBatches as $rcIdBatch ) {
182 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__
);
183 if ( count( $rcIdBatches ) > 1 ) {
184 $lbFactory->commitAndWaitForReplication(
185 __METHOD__
, $this->ticket
, [ 'domain' => $dbw->getDomainID() ]
190 // Commit and release the lock (if set)
191 ScopedCallback
::consume( $scopedLock );
194 private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
195 $services = MediaWikiServices
::getInstance();
196 $lbFactory = $services->getDBLoadBalancerFactory();
197 $dbw = $this->getDB(); // convenience
199 $res = $dbw->select( $table, $pk, $conds, __METHOD__
);
202 foreach ( $res as $row ) {
203 $pkDeleteConds[] = $dbw->makeList( (array)$row, LIST_AND
);
204 if ( count( $pkDeleteConds ) >= $bSize ) {
205 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR
), __METHOD__
);
206 $lbFactory->commitAndWaitForReplication(
207 __METHOD__
, $this->ticket
, [ 'domain' => $dbw->getDomainID() ]
213 if ( $pkDeleteConds ) {
214 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR
), __METHOD__
);
218 protected function getDB() {
220 $this->db
= wfGetDB( DB_MASTER
);
226 public function getAsJobSpecification() {
228 'wiki' => WikiMap
::getWikiIdFromDomain( $this->getDB()->getDomainID() ),
229 'job' => new JobSpecification(
231 [ 'pageId' => $this->pageId
, 'timestamp' => $this->timestamp
],
232 [ 'removeDuplicates' => true ],
233 $this->page
->getTitle()